import {
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  Length,
  Matches,
  Max,
  Min,
} from 'class-validator';
import { Transform } from 'class-transformer';

export class CreateCommentDto {
  @Length(1, 255, { message: '评论内容长度为1-255' })
  @IsString({ message: '内容必须为字符串' })
  @IsNotEmpty({ message: '评论内容不能为空' })
  content: string;

  @IsNumber({}, { message: '文章id必须为数字' })
  @IsNotEmpty({ message: '文章id不能为空' })
  @Transform((params) => Number(params.value))
  articleId: number;

  @IsNumber({}, { message: '父评论ID必须为数字' })
  @Transform((params) => Number(params.value))
  @IsOptional()
  parentId: number;
}

export class QueryCommentListDto {
  @Min(1, { message: '不能小于1' })
  @IsNumber({}, { message: '必须为数字' })
  @Transform((params) => Number(params.value))
  pageNo: number = 1;

  @Max(100, { message: '不能大于100' })
  @Min(1, { message: '不能小于1' })
  @IsNumber({}, { message: '必须为数字' })
  @Transform((params) => Number(params.value))
  pageSize: number = 10;

  @Matches(RegExp('ASC|DESC'), { message: '排序方式错误' })
  @Transform((params) => String(params.value).toUpperCase())
  @IsOptional()
  sort?: 'ASC' | 'DESC' = 'DESC';
}
